package com.main;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.hccp.net.CookieManager;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.model.CardTable;
import com.model.Fakecard;
import com.model.LoginStatus;
import com.model.UserInfo;
import com.model.User_Card;
import com.swtdesigner.SWTResourceManager;
import com.thread.FairybattleThread;
import com.util.PropertiesUtil;
import com.util.TextVerifyListener;

public class View {

	private Label label_9;
	private Label label_12;
	private Label label_11;
	private Label label_10;
	private Label label_6;
	private Label label_8;
	private Label label_7;
	private Label cardNum;
	private Label label_friend;
	private Label label_gacha_ticket;
	private Label label_fsp;
	private Label label_gold;
	private Button btn_update_area;
	private Label lable_show_bc;
	private Label label_4;
	private Label lable_show_ap;
	private Label label_5;
	private Label label_3;
	private Label label_2;
	private Label lable_remain_rewards;
	private Button button_2;
	private Label label_1;
	private Label label;
	private Button btn_update_usercard_;
	private Button btn_update_fairylist;
	private Button btn_auto_battle;
	private Button button_1;
	private Button button;
	private Button btn_logout;
	private Button btn_login;
	private Text text_lessHP;
	private Table usercardtable;
	private Table fairytable;
	private Text text_lessap;
	private Text text_lessbc;
	private Text text_main_show;
	private Combo combo_floor;
	private Combo combo_area;
	private Text text_password;
	private Text text_acount;
	protected Shell shell;

	private CardTable ct;
	private String COOKIE = "Cookie";
	private String SET_COOKIE = "Set-Cookie";
	private String USER_AGENT = "User-Agent";
	private String user_agent = "Million/100 (GT-N7000; GT-N7000; 2.3.4) samsung/GT-N7000/GT-N7000:2.3.4/GRJ22/eng.build.20120314.185218:eng/release-keys";
	private DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	private CookieManager cm = new CookieManager();
	private Document doc = null;

	private String errorMSG = "";
	private String session_id = "";

	private boolean isLogin = false;

	private DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	private String remain_rewards="";
	private NodeList fairy_event;
	private PropertiesUtil prop=new PropertiesUtil();
	private UserInfo userinfo=null;
	
	
	

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			View window = new View();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {

		HttpURLConnection.setFollowRedirects(true);
		HttpURLConnection connection_pre = null;

		shell = new Shell();
		shell.setSize(970, 656);
		shell.setText("MA黑科技——n_n自编");

		label = new Label(shell, SWT.NONE);
		label.setText("账号：");
		label.setBounds(10, 10, 31, 17);

		label_1 = new Label(shell, SWT.NONE);
		label_1.setText("密码：");
		label_1.setBounds(10, 47, 31, 17);

		btn_logout = new Button(shell, SWT.NONE);
		btn_login = new Button(shell, SWT.NONE);
		text_main_show = new Text(shell, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI
				| SWT.BORDER);
		text_password = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		text_acount = new Text(shell, SWT.BORDER);
		text_lessbc = new Text(shell, SWT.BORDER);
		text_lessap = new Text(shell, SWT.BORDER);
		button_2 = new Button(shell, SWT.NONE);
		btn_auto_battle = new Button(shell, SWT.NONE);
		btn_auto_battle.setEnabled(false);
		btn_update_fairylist = new Button(shell, SWT.NONE);
		btn_update_usercard_ = new Button(shell, SWT.NONE);
		lable_remain_rewards = new Label(shell, SWT.NONE);
		label_2 = new Label(shell, SWT.NONE);
		label_3 = new Label(shell, SWT.NONE);
		label_5 = new Label(shell, SWT.NONE);
		lable_show_ap = new Label(shell, SWT.NONE);
		label_4 = new Label(shell, SWT.NONE);
		lable_show_bc = new Label(shell, SWT.NONE);
		btn_update_area = new Button(shell, SWT.NONE);
		btn_update_area.setEnabled(false);
		button = new Button(shell, SWT.CHECK);
		button_1 = new Button(shell, SWT.CHECK);
		label_gold = new Label(shell, SWT.RIGHT);
		label_fsp = new Label(shell, SWT.RIGHT);
		label_gacha_ticket = new Label(shell, SWT.RIGHT);
		label_friend = new Label(shell, SWT.CENTER);
		cardNum = new Label(shell, SWT.CENTER);
		text_lessHP = new Text(shell, SWT.BORDER);
		text_lessHP.setEnabled(false);
		usercardtable = new Table(shell, SWT.BORDER);
		fairytable = new Table(shell, SWT.BORDER);
		
		
		fairytable.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(final MouseEvent e) {
				TableItem[] itemList =fairytable.getItems();
				 int listHaveChouse = fairytable.getSelectionIndex();
				 if(listHaveChouse>=0){
					 System.out.println(itemList[listHaveChouse].getText(5)+" "+itemList[listHaveChouse].getText(6));
//					 fairybattle(itemList[listHaveChouse]);
					 FairybattleThread fbt=new FairybattleThread(text_main_show, COOKIE,itemList[listHaveChouse],fairytable,listHaveChouse,userinfo.getCurrent_bc());
					 new Thread(fbt).start();
				 }
			}
		});
		
		btn_auto_battle.addMouseListener(new MouseAdapter() {
			public void mouseUp(final MouseEvent e) {
				Thread abt=new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});

		text_acount.setTextLimit(11);
		text_acount.setBounds(54, 7, 115, 25);
		// text.addVerifyListener(new TextVerifyListener(1));
		text_acount.setText("15112279286");

		
		btn_update_usercard_.addMouseListener(new MouseAdapter() {
			public void mouseUp(final MouseEvent e) {
				ArrayList<User_Card> usercardlist=userinfo.getOwner_card_list();
				for (int i=0;i<usercardlist.size();) {
					User_Card user_Card = (User_Card) usercardlist.get(i);
//					User_Card tempcard=new User_Card(user_Card);	
					User_Card tempcard=user_Card;
					for (int j=0;j<usercardlist.size();j++) {
						User_Card user_Card2 = (User_Card) usercardlist.get(j);
						if (Integer.parseInt(prop.getCardStarLv(tempcard.getMaster_card_id()))<Integer.parseInt(prop.getCardStarLv(user_Card2.getMaster_card_id()))) {
							tempcard=user_Card2;
						}
					}
					String serial_id=tempcard.getSerial_id();
					String card_id=tempcard.getMaster_card_id();
					String name=prop.getCardName(card_id);
					String startlv=prop.getCardStarLv(card_id);
					String lv=tempcard.getLv();
					String lv_max=tempcard.getLv_max();
					String hp=tempcard.getHp();
					String atk=tempcard.getPower();
					String sale_price=tempcard.getSale_price();
					String Limit_over=tempcard.getLimit_over();
					String unLimit_max=(Integer.parseInt(tempcard.getPlus_limit_count())+Integer.parseInt(Limit_over))+"";
					TableItem tit= new TableItem(usercardtable,SWT.CENTER);
					tit.setText(new String[]{serial_id,card_id,name,lv+"/"+lv_max,hp,atk,sale_price,Limit_over+"/"+unLimit_max});
					usercardlist.remove(tempcard);
				}
			}
		});
		
		text_password.addKeyListener(new KeyAdapter() {
			public void keyReleased(final KeyEvent e) {
				if (e.keyCode == 13 && !isLogin) {
					if (text_acount.getText().length() < 11) {
						text_main_show.append("[" + df.format(new Date())
								+ "]:用户名错误\n");
					} else if (text_password.getText().equals("")
							|| text_password.getText().length() <= 0) {
						text_main_show.append("[" + df.format(new Date())
								+ "]:密码不能为空\n");
					} else {
						errorMSG="";
						while (!Login(text_acount.getText(),
								text_password.getText())) {
							if (!errorMSG.equals("")) {
								text_main_show.append("[" + df.format(new Date())
										+ "]:" + errorMSG + "\n");
//								text_main_show.append("[" + df.format(new Date())
//										+ "]:" + "登录失败，请重新尝试...\n");
								break;
							}else{
								text_main_show.append("[" + df.format(new Date())
										+ "]:重连...\n");
							}
						}
						text_main_show.append("[" + df.format(new Date())
								+ "]:登录成功！\n");
						text_main_show.append("[" + df.format(new Date()) + "]:"
								+ "cookie:" + COOKIE+ "\n");
						isLogin = true;
						btn_login.setEnabled(false);
						btn_logout.setEnabled(true);
						text_acount.setEnabled(false);
						text_password.setEnabled(false);
						userinfo=get_userinfo();
						String max_ap=userinfo.getMax_ap();
						String max_bc=userinfo.getMax_bc();
						String current_ap=userinfo.getCurrent_ap();
						String current_bc=userinfo.getCurrent_bc();
						String gold=userinfo.getGold();
						String friendship_point=userinfo.getFriendship_point();
						String friends=userinfo.getFriends();
						String friend_max=userinfo.getFriend_max();
						String gacha_ticket=userinfo.getGacha_ticket();
						lable_show_ap.setText(current_ap+"/"+max_ap);
						lable_show_bc.setText(current_bc+"/"+max_bc);
						label_gold.setText(gold);
						label_fsp.setText(friendship_point);
						label_friend.setText(friends+"/"+friend_max);
						label_gacha_ticket.setText(gacha_ticket);
						int ownCardNum=userinfo.getOwner_card_list().size();
						
						cardNum.setText(ownCardNum+"/200");
						String fairySelect_result="";
						while(!(fairySelect_result=fairySelect()).equals("success")){
							if(fairySelect_result.equals("noLogin")){
								while (!Login(text_acount.getText(),
										text_password.getText())) {
									if (!errorMSG.equals("")) {
										text_main_show.append("[" + df.format(new Date())
												+ "]:" + errorMSG + "\n");
//										text_main_show.append("[" + df.format(new Date())
//												+ "]:" + "登录失败，请重新尝试...\n");
										break;
									}else{
										text_main_show.append("[" + df.format(new Date())
												+ "]:重连...\n");
									}
								}
							}else if(fairySelect_result.equals("false")){
								text_main_show.append("[" + df.format(new Date())
										+ "]:请重新获取妖精信息...\n");
								break;
							}
						}
						lable_remain_rewards.setText(remain_rewards);
						fairytable.removeAll();
						for(int i=0;i<fairy_event.getLength();i++){
							 String find_username=fairy_event.item(i).getChildNodes().item(0).getChildNodes().item(1).getTextContent();
							 String fairy_name=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(1).getTextContent();
							 String fairy_lv=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(2).getTextContent();
							 String fairy_hp="--/--";
							 String escape_time_second=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(3).getTextContent();
							 String put_down=fairy_event.item(i).getChildNodes().item(2).getTextContent();
							 String find_user_id=fairy_event.item(i).getChildNodes().item(0).getChildNodes().item(0).getTextContent();
							 String fairy_id=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(0).getTextContent();
							 String escape_time;
							 if(escape_time_second.equals("0")||put_down.equals("2")){
								 escape_time="已逃走";
							 }else{
								 TableItem tit= new TableItem(fairytable,SWT.CENTER);
								 int h = Integer.parseInt(escape_time_second) / 3600;
								 int m = (Integer.parseInt(escape_time_second) % 3600) / 60;
								 int s = Integer.parseInt(escape_time_second) % 60;
								 escape_time=h+":"+m+":"+s;
								 tit.setText(new String[]{fairy_name,fairy_lv,fairy_hp,find_username,escape_time,find_user_id,fairy_id});
							 }
							 
						}
					}
				}
			}
		});
		text_password.setBounds(54, 44, 115, 25);
		text_password.setText("bcg1989710");

		btn_login.addMouseListener(new MouseAdapter() {
			public void mouseUp(final MouseEvent e) {
				if (text_acount.getText().length() < 11) {
					text_main_show.append("[" + df.format(new Date())
							+ "]:用户名错误\n");
				} else if (text_password.getText().equals("")
						|| text_password.getText().length() <= 0) {
					text_main_show.append("[" + df.format(new Date())
							+ "]:密码不能为空\n");
				} else {
					errorMSG="";
					while (!Login(text_acount.getText(),
							text_password.getText())) {
						if (!errorMSG.equals("")) {
							text_main_show.append("[" + df.format(new Date())
									+ "]:" + errorMSG + "\n");
//							text_main_show.append("[" + df.format(new Date())a
//									+ "]:" + "登录失败，请重新尝试...\n");
							break;
						}else{
							text_main_show.append("[" + df.format(new Date())
									+ "]:重连...\n");
						}
					}
					text_main_show.append("[" + df.format(new Date())
							+ "]:登录成功！\n");
					text_main_show.append("[" + df.format(new Date()) + "]:"
							+ "cookie:" + COOKIE+ "\n");
					isLogin = true;
					btn_login.setEnabled(false);
					btn_logout.setEnabled(true);
					text_acount.setEnabled(false);
					text_password.setEnabled(false);
					userinfo=get_userinfo();
					String max_ap=userinfo.getMax_ap();
					String max_bc=userinfo.getMax_bc();
					String current_ap=userinfo.getCurrent_ap();
					String current_bc=userinfo.getCurrent_bc();
					String gold=userinfo.getGold();
					String friendship_point=userinfo.getFriendship_point();
					String friends=userinfo.getFriends();
					String friend_max=userinfo.getFriend_max();
					String gacha_ticket=userinfo.getGacha_ticket();
					lable_show_ap.setText(current_ap+"/"+max_ap);
					lable_show_bc.setText(current_bc+"/"+max_bc);
					label_gold.setText(gold);
					label_fsp.setText(friendship_point);
					label_friend.setText(friends+"/"+friend_max);
					label_gacha_ticket.setText(gacha_ticket);
					int ownCardNum=userinfo.getOwner_card_list().size();
					
					cardNum.setText(ownCardNum+"/200");
					String fairySelect_result="";
					while(!(fairySelect_result=fairySelect()).equals("success")){
						if(fairySelect_result.equals("noLogin")){
							while (!Login(text_acount.getText(),
									text_password.getText())) {
								if (!errorMSG.equals("")) {
									text_main_show.append("[" + df.format(new Date())
											+ "]:" + errorMSG + "\n");
//									text_main_show.append("[" + df.format(new Date())
//											+ "]:" + "登录失败，请重新尝试...\n");
									break;
								}else{
									text_main_show.append("[" + df.format(new Date())
											+ "]:重连...\n");
								}
							}
						}else if(fairySelect_result.equals("false")){
							text_main_show.append("[" + df.format(new Date())
									+ "]:请重新获取妖精信息...\n");
							break;
						}
					}
					lable_remain_rewards.setText(remain_rewards);
					fairytable.removeAll();
					for(int i=0;i<fairy_event.getLength();i++){
						 String find_username=fairy_event.item(i).getChildNodes().item(0).getChildNodes().item(1).getTextContent();
						 String fairy_name=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(1).getTextContent();
						 String fairy_lv=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(2).getTextContent();
						 String fairy_hp="--/--";
						 String escape_time_second=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(3).getTextContent();
						 String put_down=fairy_event.item(i).getChildNodes().item(2).getTextContent();
						 String find_user_id=fairy_event.item(i).getChildNodes().item(0).getChildNodes().item(0).getTextContent();
						 String fairy_id=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(0).getTextContent();
						 String escape_time;
						 if(escape_time_second.equals("0")||put_down.equals("2")){
							 escape_time="已逃走";
						 }else{
							 TableItem tit= new TableItem(fairytable,SWT.CENTER);
							 int h = Integer.parseInt(escape_time_second) / 3600;
							 int m = (Integer.parseInt(escape_time_second) % 3600) / 60;
							 int s = Integer.parseInt(escape_time_second) % 60;
							 escape_time=h+":"+m+":"+s;
							 tit.setText(new String[]{fairy_name,fairy_lv,fairy_hp,find_username,escape_time,find_user_id,fairy_id});
						 }
						 
					}
				}
			}
		});
		btn_login.setText("登录");
		btn_login.setBounds(175, 5, 50, 27);
		
		btn_logout.addMouseListener(new MouseAdapter() {
			public void mouseUp(final MouseEvent e) {
				isLogin = false;
				btn_login.setEnabled(true);
				btn_logout.setEnabled(false);
				text_acount.setEnabled(true);
				text_password.setEnabled(true);
			}
		});
		btn_logout.setText("注销");
		btn_logout.setBounds(175, 42, 50, 27);

		btn_update_fairylist.addMouseListener(new MouseAdapter() {
			public void mouseUp(final MouseEvent e) {
				fairytable.removeAll();
				String fairySelect_result="";
				while(!(fairySelect_result=fairySelect()).equals("success")){
					if(fairySelect_result.equals("noLogin")){
						if(isEntryAcAndPwd().equals("noAc")){
							text_main_show.append("[" + df.format(new Date())
									+ "]:自动连接错误：未填写账号\n");
							return;
						}
						if(isEntryAcAndPwd().equals("noPwd")){
							text_main_show.append("[" + df.format(new Date())
									+ "]:自动连接错误：未填写密码\n");
							return;
						}
						if(isEntryAcAndPwd().equals("")){
							text_main_show.append("[" + df.format(new Date())
									+ "]:当前未连接，正在尝试自动连接...\n");
							Login(text_acount.getText(),text_password.getText());
							if ((fairySelect_result=fairySelect()).equals("success")) {
								text_main_show.append("[" + df.format(new Date())
										+ "]:自动重连成功，正在进行妖精查询。\n");
								break;
							}else{
								text_main_show.append("[" + df.format(new Date())
										+ "]:自动连接失败，请手动连接。\n");
							}
						}
					}else if(fairySelect_result.equals("false")){
						text_main_show.append("[" + df.format(new Date())
								+ "]:请重新获取妖精信息...\n");
						break;
					}
				}
				lable_remain_rewards.setText(remain_rewards);
				int ablefairyNum=0;
				for(int i=0;i<fairy_event.getLength();i++){
					 String find_username=fairy_event.item(i).getChildNodes().item(0).getChildNodes().item(1).getTextContent();
					 String fairy_name=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(1).getTextContent();
					 String fairy_lv=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(2).getTextContent();
					 String fairy_hp="--/--";
					 String escape_time_second=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(3).getTextContent();
					 String put_down=fairy_event.item(i).getChildNodes().item(2).getTextContent();
					 String find_user_id=fairy_event.item(i).getChildNodes().item(0).getChildNodes().item(0).getTextContent();
					 String fairy_id=fairy_event.item(i).getChildNodes().item(1).getChildNodes().item(0).getTextContent();
					 String escape_time;
					 if(escape_time_second.equals("0")||put_down.equals("2")){
						 escape_time="已逃走";
					 }else{
						 TableItem tit= new TableItem(fairytable,SWT.CENTER);
						 int h = Integer.parseInt(escape_time_second) / 3600;
						 int m = (Integer.parseInt(escape_time_second) % 3600) / 60;
						 int s = Integer.parseInt(escape_time_second) % 60;
						 escape_time=h+":"+m+":"+s;
						 tit.setText(new String[]{fairy_name,fairy_lv,fairy_hp,find_username,escape_time,find_user_id,fairy_id});
						 ablefairyNum++;
					 }
				}
				text_main_show.append("[" + df.format(new Date())
						+ "]:当前可攻击妖精数为:"+ablefairyNum+"\n");
			}
		});
		
		combo_area = new Combo(shell, SWT.READ_ONLY);
		combo_area.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NONE));
		combo_area.setBounds(298, 10, 158, 25);

		combo_floor = new Combo(shell, SWT.READ_ONLY);
		combo_floor.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NONE));
		combo_floor.setBounds(460, 10, 101, 20);

		text_main_show.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NONE));
		text_main_show.setBounds(10, 122, 459, 486);
		text_main_show.setText("");

		
		label_2.setText("AP≥");
		label_2.setBounds(506, 52, 31, 17);

		
		label_3.setText("BC≥");
		label_3.setBounds(594, 52, 31, 17);

		text_lessbc.setText("2");
		text_lessbc.setBounds(629, 47, 42, 25);
		text_lessbc.addVerifyListener(new TextVerifyListener(1));
		text_lessbc.setText("");

		text_lessap.setEnabled(false);
		text_lessap.setText("120");
		text_lessap.setBounds(539, 47, 42, 25);
		text_lessap.addVerifyListener(new TextVerifyListener(1));
		text_lessap.setText("");
		
		label_5.setText("AP：");
		label_5.setBounds(283, 52, 27, 17);
		
		lable_show_ap.setAlignment(SWT.CENTER);
		lable_show_ap.setText("0/0");
		lable_show_ap.setBounds(316, 52, 49, 17);
		
		label_4.setText("BC：");
		label_4.setBounds(375, 52, 31, 17);
		
		lable_show_bc.setBounds(408, 52, 50, 17);
		lable_show_bc.setAlignment(SWT.CENTER);
		lable_show_bc.setText("0/0");

		button_2.setEnabled(false);
		button_2.setText("自动跑图");
		button_2.setBounds(815, 5, 61, 27);

		btn_auto_battle.setText("自动打怪");
		btn_auto_battle.setBounds(724, 5, 63, 27);
		
		btn_update_area.setText("更新地图");
		btn_update_area.setBounds(231, 5, 61, 27);
		
		button.setEnabled(false);
		button.setText("自动换区");
		button.setBounds(573, 10, 69, 17);
		
		button_1.setEnabled(false);
		button_1.setText("放妖");
		button_1.setBounds(648, 10, 50, 17);

		fairytable.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NONE));
		fairytable.setLinesVisible(true);
		fairytable.setHeaderVisible(true);
		fairytable.setBounds(475, 77, 462, 266);
		
		final TableColumn newColumnTableColumn = new TableColumn(fairytable,
				SWT.CENTER);
		newColumnTableColumn.setWidth(106);
		newColumnTableColumn.setText("妖精名字");

		final TableColumn newColumnTableColumn_1 = new TableColumn(fairytable,
				SWT.CENTER);
		newColumnTableColumn_1.setWidth(63);
		newColumnTableColumn_1.setText("妖精等级");

		final TableColumn newColumnTableColumn_3 = new TableColumn(fairytable,
				SWT.CENTER);
		newColumnTableColumn_3.setWidth(122);
		newColumnTableColumn_3.setText("当前血量");

		final TableColumn newColumnTableColumn_2 = new TableColumn(fairytable,
				SWT.CENTER);
		newColumnTableColumn_2.setWidth(72);
		newColumnTableColumn_2.setText("发现者");

		final TableColumn newColumnTableColumn_4 = new TableColumn(fairytable,
				SWT.CENTER);
		newColumnTableColumn_4.setWidth(69);
		newColumnTableColumn_4.setText("逃跑时间");

		final TableColumn newColumnTableColumn_14 = new TableColumn(fairytable, SWT.NONE);
		newColumnTableColumn_14.setWidth(100);
		newColumnTableColumn_14.setText("发现者id");

		final TableColumn newColumnTableColumn_13 = new TableColumn(fairytable, SWT.NONE);
		newColumnTableColumn_13.setWidth(100);
		newColumnTableColumn_13.setText("妖精id");

		btn_update_fairylist.setText("刷新妖精列表");
		btn_update_fairylist.setBounds(849, 42, 88, 27);

		btn_update_usercard_.setText("刷新卡片列表");
		btn_update_usercard_.setBounds(849, 361, 88, 27);

		usercardtable.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NONE));
		usercardtable.setBounds(475, 394, 462, 214);
		usercardtable.setLinesVisible(true);
		usercardtable.setHeaderVisible(true);

		final TableColumn newColumnTableColumn_5 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_5.setWidth(74);
		newColumnTableColumn_5.setText("serial_id");

		final TableColumn newColumnTableColumn_6 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_6.setWidth(56);
		newColumnTableColumn_6.setText("card_id");

		final TableColumn newColumnTableColumn_12 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_12.setWidth(67);
		newColumnTableColumn_12.setText("卡名");

		final TableColumn newColumnTableColumn_7 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_7.setWidth(62);
		newColumnTableColumn_7.setText("lv");

		final TableColumn newColumnTableColumn_8 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_8.setWidth(59);
		newColumnTableColumn_8.setText("HP");

		final TableColumn newColumnTableColumn_9 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_9.setWidth(67);
		newColumnTableColumn_9.setText("ATK");

		final TableColumn newColumnTableColumn_11 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_11.setWidth(61);
		newColumnTableColumn_11.setText("贩卖价格");

		final TableColumn newColumnTableColumn_10 = new TableColumn(
				usercardtable, SWT.NONE);
		newColumnTableColumn_10.setWidth(62);
		newColumnTableColumn_10.setText("突破次数");

		label_7 = new Label(shell, SWT.NONE);
		label_7.setText("尾刀血量≤");
		label_7.setBounds(690, 52, 61, 17);

		text_lessHP.setText("5000");
		text_lessHP.setBounds(757, 47, 80, 25);
		text_lessHP.addVerifyListener(new TextVerifyListener(1));
		text_lessHP.setText("");

		label_8 = new Label(shell, SWT.NONE);
		label_8.setText("当前未领卡牌数：");
		label_8.setBounds(283, 80, 96, 17);

		
		lable_remain_rewards.setText("0");
		lable_remain_rewards.setBounds(385, 81, 50, 17);

		label_6 = new Label(shell, SWT.NONE);
		label_6.setText("金币：");
		label_6.setBounds(10, 81, 31, 17);

		
		label_gold.setText("0");
		label_gold.setBounds(47, 81, 61, 17);

		label_10 = new Label(shell, SWT.NONE);
		label_10.setText("友情点：");
		label_10.setBounds(10, 104, 50, 17);
		
		label_fsp.setText("0");
		label_fsp.setBounds(66, 104, 42, 17);

		label_11 = new Label(shell, SWT.NONE);
		label_11.setText("蛋券：");
		label_11.setBounds(156, 81, 31, 17);
		
		label_gacha_ticket.setText("0");
		label_gacha_ticket.setBounds(193, 81, 31, 17);

		label_12 = new Label(shell, SWT.NONE);
		label_12.setText("好友：");
		label_12.setBounds(156, 104, 31, 17);
		
		label_friend.setText("0/0");
		label_friend.setBounds(193, 104, 53, 17);

		label_9 = new Label(shell, SWT.NONE);
		label_9.setText("当前卡牌数：");
		label_9.setBounds(279, 103, 69, 17);

		
		cardNum.setText("0/200");
		cardNum.setBounds(354, 104, 52, 17);
		shell.setTabList(new Control[] {text_acount, text_password, btn_login, btn_logout, combo_area, combo_floor, text_lessap, text_lessbc, text_lessHP, button, button_1, btn_auto_battle, btn_update_fairylist, btn_update_usercard_, label, label_1, text_main_show, button_2, lable_remain_rewards, label_2, label_3, label_5, lable_show_ap, label_4, lable_show_bc, btn_update_area, label_gold, label_fsp, label_gacha_ticket, label_friend, cardNum, usercardtable, fairytable, label_7, label_8, label_6, label_10, label_11, label_12, label_9});

		//
		//
		// combo_1.removeAll();
		// combo.removeAll();
		//
		// for(int i=0;i<20;i++){
		// combo_1.add("第"+i+"个地图的名字", i);
		// }
		// combo_1.select(0);
		//
		// for(int i=0;i<10;i++){
		// combo.add("区域"+i+"|5|100", i);
		// }
		// combo.select(0);
		//
		// for(int i=0;i<20;i++){
		// TableItem tit= new TableItem(table,SWT.CENTER);
		// tit.setText(new
		// String[]{"觉醒的一二三四五","120","1000000/2000000","觉醒的二三四","29:59"});
		// }
		//
		// for(int i=0;i<20;i++){
		// TableItem tit= new TableItem(table_2,SWT.CENTER);
		// tit.setText(new
		// String[]{"123456789","120","100/100","100000","100000","10","130000"});
		// }
		//
		// for(int i=0;i<200;i++){
		// text_2.append("[2013/8/25 21:47:13]移动：南方十字的海滨|1|2|100-发现妖精lv85琪妮安 HP:677121  AP:30-28 BC:104-104\n");
		// }
		// text_2.setTopIndex(text_2.getLineCount());
		// ct=new CardTable();
		// java.util.Iterator it = ct.getCardtable().entrySet().iterator();
		// while(it.hasNext()){
		// java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
		// text_2.append(entry.getKey()+" "+entry.getValue()+"\n");
		// }
		// text_2.setTopIndex(text_2.getLineCount());
	}
	
	private boolean fairybattle(TableItem tit){
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			this.text_main_show.append("[" + df.format(new Date())
					+ "]:攻击"+tit.getText(3)+"→lv"+tit.getText(1)+tit.getText(0)+"\n");
			URL urlLog = new URL(
					"http://117.121.6.138:10001/connect/app/exploration/fairybattle?serial_id="+tit.getText(6)+"&user_id="+tit.getText(5));
			HttpURLConnection connection = (HttpURLConnection) urlLog
					.openConnection();
			connection.setRequestMethod("POST");
			connection.addRequestProperty("Cookie", COOKIE);
			InputStream is = connection.getInputStream();
			
			doc = db.parse(is);
			
			tit.setText(2, doc.getElementsByTagName("explore").item(0).getChildNodes().item(2).getChildNodes().item(4).getTextContent()+"/"+doc.getElementsByTagName("explore").item(0).getChildNodes().item(2).getChildNodes().item(5).getTextContent());
			String escape_time_second=doc.getElementsByTagName("explore").item(0).getChildNodes().item(2).getChildNodes().item(6).getTextContent();
			int h = Integer.parseInt(escape_time_second) / 3600;
			 int m = (Integer.parseInt(escape_time_second) % 3600) / 60;
			 int s = Integer.parseInt(escape_time_second) % 60;
			String escape_time=h+":"+m+":"+s;
			tit.setText(4, escape_time);
//			fairy_event=doc.getElementsByTagName("fairy_event");
//			remain_rewards=doc.getElementsByTagName("remaining_rewards").item(0).getTextContent();
			
			return true;
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return false;
		}catch(NullPointerException e){
			e.printStackTrace();
			return false;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	private String fairySelect() {
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			URL urlLog = new URL(
					"http://117.121.6.138:10001/connect/app/menu/fairyselect?cyt=1");
			HttpURLConnection connection = (HttpURLConnection) urlLog
					.openConnection();
			connection.setRequestMethod("POST");
			connection.addRequestProperty("Cookie", COOKIE);
			InputStream is = connection.getInputStream();

			doc = db.parse(is);
			fairy_event=doc.getElementsByTagName("fairy_event");
			remain_rewards=doc.getElementsByTagName("remaining_rewards").item(0).getTextContent();
			
			return "success";
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return "noLogin";
		}catch(NullPointerException e){
			e.printStackTrace();
			return "noLogin";
		}catch(Exception e){
			e.printStackTrace();
			return "false";
		}
	}

	private boolean Login(String account, String password) {
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();

			System.out.print("进程1/4...");
			URL urlLog = new URL(
					"http://game1-CBT.ma.sdo.com:10001/connect/app/login?&login_id="
							+ account + "&password=" + password);
			HttpURLConnection.setFollowRedirects(false);
			HttpURLConnection connection_pre = (HttpURLConnection) urlLog
					.openConnection();

			InputStream is = connection_pre.getInputStream();
			COOKIE = connection_pre.getHeaderField("Set-Cookie");
			System.out.println("Cookie:" + COOKIE + " "
					+ connection_pre.getResponseMessage());

			HttpURLConnection.setFollowRedirects(true);

			System.out.print("进程2/4...");
			urlLog = new URL(
					"http://game1-CBT.ma.sdo.com:10001/connect/app/login?&login_id="
							+ account + "&password=" + password);
			HttpURLConnection connection = (HttpURLConnection) urlLog
					.openConnection();
			connection.setRequestMethod("POST");
			connection.addRequestProperty("Cookie", COOKIE);
			is = connection.getInputStream();

			doc = db.parse(is);

			NodeList nodes = doc.getChildNodes();
			System.out.println(nodes.getLength());
			System.out.println(nodes.item(0).getNodeType());
			System.out.println(nodes.item(0).getChildNodes().getLength());
			getTree(nodes.item(0), 0);
			String error = doc.getElementsByTagName("error").item(0)
					.getTextContent();
			if (!error.equals("0")) {
				this.errorMSG = error;
				return false;
			}
			session_id = doc.getElementsByTagName("session_id").item(0)
					.getTextContent();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public UserInfo get_userinfo() {
		UserInfo userinfo = new UserInfo();
		userinfo.setSession_id(session_id);
		userinfo.setGold((doc.getElementsByTagName("gold").item(0).getTextContent()));
		userinfo.setName(doc.getElementsByTagName("name").item(0)
				.getTextContent());
		userinfo.setLeader_serial_id(doc
				.getElementsByTagName("leader_serial_id").item(0)
				.getTextContent());
		userinfo.setTown_level(doc.getElementsByTagName("town_level").item(0)
				.getTextContent());
		userinfo.setPercentage(doc.getElementsByTagName("percentage").item(0)
				.getTextContent());
		userinfo.setCurrent_ap(doc.getElementsByTagName("ap").item(0)
				.getChildNodes().item(0).getTextContent());
		userinfo.setMax_ap(doc.getElementsByTagName("ap").item(0)
				.getChildNodes().item(1).getTextContent());
		userinfo.setLast_update_time_ap(doc.getElementsByTagName("ap").item(0)
				.getChildNodes().item(3).getTextContent());
		userinfo.setCurrent_bc(doc.getElementsByTagName("bc").item(0)
				.getChildNodes().item(0).getTextContent());
		userinfo.setMax_bc(doc.getElementsByTagName("bc").item(0)
				.getChildNodes().item(1).getTextContent());
		userinfo.setLast_update_time_bc(doc.getElementsByTagName("bc").item(0)
				.getChildNodes().item(3).getTextContent());
		userinfo.setMax_card_num(doc.getElementsByTagName("max_card_num")
				.item(0).getTextContent());
		userinfo.setFree_ap_bc_point(doc
				.getElementsByTagName("free_ap_bc_point").item(0)
				.getTextContent());
		userinfo.setFriendship_point(doc
				.getElementsByTagName("friendship_point").item(0)
				.getTextContent());
		userinfo.setEx_gauge(doc.getElementsByTagName("ex_gauge").item(0)
				.getTextContent());
		userinfo.setGacha_ticket(doc.getElementsByTagName("gacha_ticket")
				.item(0).getTextContent());
		userinfo.setDeck_rank(doc.getElementsByTagName("deck_rank").item(0)
				.getTextContent());
		userinfo.setFriends(doc.getElementsByTagName("friends").item(0)
				.getTextContent());
		userinfo.setFriend_max(doc.getElementsByTagName("friend_max").item(0)
				.getTextContent());
		userinfo.setFriends_invitations(doc
				.getElementsByTagName("friends_invitations").item(0)
				.getTextContent());
		userinfo.setNotice_of_menu(doc.getElementsByTagName("notice_of_menu")
				.item(0).getTextContent());
		userinfo.setGacha_point(doc.getElementsByTagName("gacha_point").item(0)
				.getTextContent());
		userinfo.setFairy_appearance(doc
				.getElementsByTagName("fairy_appearance").item(0)
				.getTextContent());
		userinfo.setOwner_card_list(getUserCardLsit());
		userinfo.setItemlist(getItemlist());
		userinfo.setLogin(getLogin());
		userinfo.setFakecardlist(getFakecardlist());
		userinfo.setGreeting(doc.getElementsByTagName("greeting").item(0)
				.getTextContent());
		return userinfo;
	}

	public ArrayList<User_Card> getUserCardLsit() {
		ArrayList<User_Card> user_card_list = new ArrayList<User_Card>();
		NodeList nodes = doc.getElementsByTagName("owner_card_list").item(0)
				.getChildNodes();
		for (int i = 0; i < nodes.getLength(); i++) {
			User_Card user_card = new User_Card();
			user_card.setSerial_id(nodes.item(i).getChildNodes().item(0)
					.getTextContent());
			user_card.setMaster_card_id(nodes.item(i).getChildNodes().item(1)
					.getTextContent());
			user_card.setHolography(nodes.item(i).getChildNodes().item(2)
					.getTextContent());
			user_card.setHp(nodes.item(i).getChildNodes().item(3)
					.getTextContent());
			user_card.setPower(nodes.item(i).getChildNodes().item(4)
					.getTextContent());
			user_card.setCritical(nodes.item(i).getChildNodes().item(5)
					.getTextContent());
			user_card.setLv(nodes.item(i).getChildNodes().item(6)
					.getTextContent());
			user_card.setLv_max(nodes.item(i).getChildNodes().item(7)
					.getTextContent());
			user_card.setExp(nodes.item(i).getChildNodes().item(8)
					.getTextContent());
			user_card.setMax_exp(nodes.item(i).getChildNodes().item(9)
					.getTextContent());
			user_card.setNext_exp(nodes.item(i).getChildNodes().item(10)
					.getTextContent());
			user_card.setExp_diff(nodes.item(i).getChildNodes().item(11)
					.getTextContent());
			user_card.setExp_per(nodes.item(i).getChildNodes().item(12)
					.getTextContent());
			user_card.setSale_price(nodes.item(i).getChildNodes().item(13)
					.getTextContent());
			user_card.setMaterial_price(nodes.item(i).getChildNodes().item(14)
					.getTextContent());
			user_card.setEvolution_price(nodes.item(i).getChildNodes().item(15)
					.getTextContent());
			user_card.setPlus_limit_count(nodes.item(i).getChildNodes()
					.item(16).getTextContent());
			user_card.setLimit_over(nodes.item(i).getChildNodes().item(17)
					.getTextContent());
			user_card_list.add(user_card);
		}
		return user_card_list;
	}

	public Map<String, String> getItemlist() {
		Map<String, String> itemlist = new HashMap<String, String>();
		NodeList nodes = doc.getElementsByTagName("itemlist");
		for (int i = 0; i < nodes.getLength(); i++) {
			itemlist.put(nodes.item(i).getNodeName(), nodes.item(i)
					.getTextContent());
		}
		return itemlist;
	}

	public LoginStatus getLogin() {
		LoginStatus login = new LoginStatus();
		NodeList nodes = doc.getElementsByTagName("login");
		login.setUser_id(nodes.item(0).getChildNodes().item(0).getTextContent());
		login.setPush_fairy_yell(nodes.item(0).getChildNodes().item(1).getTextContent());
		login.setPush_good(nodes.item(0).getChildNodes().item(2).getTextContent());
		return login;
	}

	public ArrayList<Fakecard> getFakecardlist() {
		ArrayList<Fakecard> fakecardlist = new ArrayList<Fakecard>();
		NodeList nodes = doc.getElementsByTagName("fakecard");
		for (int i = 0; i < nodes.getLength(); i++) {
			Fakecard fakecard = new Fakecard();
			fakecard.setKnight_id(nodes.item(i).getChildNodes().item(0)
					.getTextContent());
			fakecard.setParts_no(nodes.item(i).getChildNodes().item(1)
					.getTextContent());
			fakecard.setCount(nodes.item(i).getChildNodes().item(2)
					.getTextContent());
			fakecardlist.add(fakecard);
		}
		return fakecardlist;
	}
	
	/**
	 * @return noAc if 没输入账号  
	 * @return noPwd if 没输入密码
	 * @return "" if 都输入了
	 */
	public String isEntryAcAndPwd(){
		String result="";
		if (text_acount.getText().equals("")) {
			result="noAc";
			return result;
		}
		if(text_password.getText().equals("")){
			result="noPwd";
			return result;
		}
		return result;
	}
	
	public String getCardName(String cardid){
		
		return "";
	}
	
	public static void getTree(Node node, int times) {
		for (int i = 0; i < times; i++) {
			System.out.print("\t");
		}
		if (node.getNodeValue() != null) {
			System.out.print(node.getNodeValue() + "\n");
		} else {
			System.out.print(node.getNodeName() + "\n");
		}
		if (node.hasChildNodes()) {
			// System.out.print("\t"+node.getNodeName()+"\n");
			for (int i = 0; i < node.getChildNodes().getLength(); i++) {
				getTree(node.getChildNodes().item(i), times + 1);
			}
		} else {
			// System.out.print("\t"+node.getNodeName()+"\n");
		}
	}
	

}
